<?php
namespace Admin\Controller;
use Think\Controller;
class RbacController extends CommonController{
	//角色列表的显示
	public function role(){
		$role=M('role')->order('id')->select();
		$this->assign('role',$role);
		$this->display();
	}
	//增加角色
	public function  addRole(){
		$this->display();
	}
	//增加角色的数据处理
	public function addRoleDo(){
		if(!IS_POST){
			$this->error('非法提交');
			exit;
		}else{
			$model=M('role');
			$data=$model->create();
			if($model->add($data)){
				$this->success('添加成功',U('Rbac/role'));
			}else{
				$this->error('添加失败');
			}
		}
	}
    //角色编辑
	public function editRole(){
		$id=I('id','','intval');
		$role=M('role')->where('id='.$id)->find();
		$this->assign('role',$role);
		$this->display();
	} 

	//对编辑数据的处理
	public function editRoleDo(){
		if(!IS_POST){
			$this->error('非法提交');
			exit;
		}else{
			$model=M('role');
			$id=I('id');
            $data=$model->create();
            if($model->where('id='.$id)->save($data)){
            	$this->success('编辑成功',U('Rbac/role'));
            }else{
            	$this->error('编辑失败');
            }
		}
	}
	//角色删除
	public function del(){
		$id=I('id');
		M('access')->where('role_id='.$id)->delete();
		$info=M('role')->where('id='.$id)->delete();
		if($info!==false){
			$this->redirect('Rbac/role');
		}else{
			$this->error('删除成功,请联系管理员');
		}
	}
    //节点列表显示
    public function node(){
    	$field=array('id,name,remark,pid,sort');
    	$node=M('node')->field($field)->order('sort desc,id desc')->select();
    	$this->assign('node',node_merge($node));
    	$this->display();
    }
    //显示添加的界面
    public function addNode(){
    	$pid=I('pid',0,'intval');
    	$level=I('level',1,'intval');
    	switch($level){
    		case 1:
    		$type="应用";
    		break;
    		case 2:
    		$type="控制器";
    		break;
    		case 3:
    		$type='方法';
    		break;
    	}
    	$this->assign(array('pid'=>$pid,'level'=>$level,'type'=>$type));
    	$this->display();
    }
    //处理添加的数据
    public function addNodeDo(){
    	if(!IS_POST){
    		$this->error('非法提交');
    		exit;
    	}else{
    		$model=M('node');
    		$data=$model->create();
    		if($model->add($data)){
    			$this->success('节点添加成功',U('Rbac/node'));
    		}else{
    			$this->error('节点添加失败');
    		}
    	}

    }
    //显示编辑界面
    public function editNode(){
    	$id=I('id');
    	$node=M('node')->where('id='.$id)->find();
    	$this->assign('node',$node);
    	$level=I('level');
    	switch($level){
    		case 1:
    		$type="应用";
    		break;
    		case 2:
    		$type="控制器";
    		break;
    		case 3:
    		$type='方法';
    		break;
    	}
    	$this->display();
    }
    //处理编辑数据
    public function editNodeDo(){
    	if(!IS_POST){
    		$this->error('非法提交');
    		exit;
    	}else{
    		$model=M('node');
    		$id=I('id');
    		$data=$model->create();
    		if($model->where('id='.$id)->save($data)){
    			$this->success('编辑成功',U('Rbac/node'));
    		}else{
    			$this->error('编辑失败');
    		}
    	}
    }

    //显示权限列表
    public function access(){
    	$rid=I('rid','','intval');
    	$field = array('id','name','remark','title','pid','sort');
    	$node=M('node')->order('sort desc,id desc')->field($field)->select();//获得所有的节点
    	$access=M('access')->where('role_id='.$rid)-> getField('node_id',true);;
    	$node=node_merge($node,$access);
    	$this->assign(array('rid'=>$rid,'node'=>$node));
    	$this->display();
    }
    //设置权限
    public function setAccess(){
		$rid = I('rid',0,'intval');
		$db = M('Access');
		//清空原来的权限
		$db -> where(array('role_id' => $rid)) -> delete();
		//组合新的权限
		$data = array();
		foreach($_POST['access'] as $v){
			$tmp = explode('_',$v);
			$data[] = array(
				'role_id' => $rid,
				'node_id' => $tmp[0],
				'level' => $tmp[1],
			);
		}
		//插入新的哦权限
		$info = $db -> addAll($data);
		if($info >= 0){
			$this -> success('修改成功',U('Rbac/role'));
		}else{
			$this -> error('修改失败');	
		}
	}


}